package test.thingsboard

import org.junit.jupiter.api.Test
import org.thingsboard.rest.client.RestClient
import org.thingsboard.server.common.data.Device
import org.thingsboard.server.common.data.page.PageData
import org.thingsboard.server.common.data.page.PageLink
import java.util.function.Consumer

class ThingsboardTest {

    @Test
    fun `test`() {
        val url = "http://192.168.18.105:9002"

        // Default Tenant Administrator credentials
        val username = "industryCustomer@mfox.cn"
        val password = "industry"

        // Creating new rest client and auth with credentials
        val client = RestClient(url)
        client.login(username, password)

        // Get information of current logged in user and print it
        client.user.ifPresent(::println)

        var tenantDevices: PageData<Device?>
        var pageLink = PageLink(10)
        do {
            // Fetch all tenant devices using current page link and print each of them
            tenantDevices = client.getTenantDevices("", pageLink)
            tenantDevices.data.forEach(Consumer { x: Device? -> println(x) })
            pageLink = pageLink.nextPageLink()
        } while (tenantDevices.hasNext())

        // Perform logout of current user and close the client
        client.logout()
        client.close()
    }
}